package com.weight.system.datagroupd.web;

import com.weight.system.datagroupd.PO.DatagroupdInfo;
import com.weight.system.datagroupd.VO.DatagroupdInfoVO;
import com.weight.system.datagroupd.service.DatagroupdInfoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 数据结构表
 */
@RestController
@Slf4j
@RequestMapping("/datagroupdInfo")
public class DatagroupdInfoController {

	@Autowired
	private DatagroupdInfoService datagroupdInfoService;

	private String pattern = "yyyy-MM-dd";


	/**
	 * 1.查询数据结构表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForDatagroupdInfoList",method=RequestMethod.GET)
	public ModelAndView queryForDatagroupdInfoList(){
			
		Map<Object, Object> map =new HashMap<>();
		map.put("dataStatus", 1);
		
		//2、查询数据结构表列表
		List<DatagroupdInfo> datagroupdInfoList = datagroupdInfoService.findDatagroupdInfoByT(map);

		log.info("datagroupdInfoList={}",datagroupdInfoList);
		List<DatagroupdInfoVO> datagroupdInfoVOList = datagroupdInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
					
		//3、返回前台
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("datagroupd/queryForDatagroupdInfoList");
		modelAndView.addObject("datagroupdInfoVOList",datagroupdInfoVOList);
		return modelAndView;
	}
	/**
	 * 3.添加、修改数据结构表操作
	 * @param dataId
	 * @return
	 */
	@RequestMapping(value="/addDatagroupdInfo")
	public ModelAndView addDatagroupdInfo(@RequestParam(value="dataId",required=false) String dataId){
		//1、创建返回对象
		DatagroupdInfoVO datagroupdInfoVO = new DatagroupdInfoVO();
		
		//2.判定id不为空，则查询数据库，为修改操作
		if(StringUtils.isNotBlank(dataId)) {
			Map<String, String> map=new HashMap<>();
			map.put("dataId", dataId);
			log.info("修改操作，查询ID：dataId={}",dataId);
			DatagroupdInfo datagroupdInfo = datagroupdInfoService.findByDatagroupdInfoId(map);
			this.changePoToVO(datagroupdInfoVO,datagroupdInfo);
		}
		
		//3.数据返回给前台
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("datagroupd/addDatagroupdInfo");
		modelAndView.addObject("datagroupdInfoVO",datagroupdInfoVO);
		return modelAndView;
	}

	/**
	 * 5.保存 添加、修改数据结构表操作
	 * @param datagroupdInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveDatagroupdInfo")
	public JsonResultUtil saveDatagroupdInfo(@Valid DatagroupdInfoVO datagroupdInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,datagroupdInfoVO);
			
			//2.获取id
			String dataId=datagroupdInfoVO.getDataId();
			DatagroupdInfo datagroupdInfo=new DatagroupdInfo();
			BeanUtils.copyProperties(datagroupdInfoVO,datagroupdInfo);
			this.changeVOToPO(datagroupdInfoVO,datagroupdInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(dataId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("dataId", dataId);
				log.info("修改操作，查询ID：dataId={}",dataId);
				//查询是否存在
				datagroupdInfoService.findByDatagroupdInfoId(map);
				try{
					datagroupdInfo.setUpdateBy("000");
					datagroupdInfoService.updateDatagroupdInfo(datagroupdInfo);
					log.info("更新操作成功：datagroupdInfo={}",datagroupdInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					datagroupdInfo.setDataId(CommonUtils.getUUId());
					datagroupdInfo.setCreateBy("000");
					datagroupdInfo.setUpdateBy("000");
					datagroupdInfo.setDataStatus(1);
					datagroupdInfoService.insertDatagroupdInfo(datagroupdInfo);
					log.info("添加操作成功：datagroupdInfo={}",datagroupdInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}
	/**
	 * 2.查询数据结构表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForDatagroupdInfoListDG",method=RequestMethod.GET)
	public String queryForDatagroupdInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String datatabId = request.getParameter("datatabId");
			String datatabEname = request.getParameter("datatabEname");
			String datatabCname = request.getParameter("datatabCname");
			String dataEname = request.getParameter("dataEname");
			String dataCname = request.getParameter("dataCname");
			String dataType = request.getParameter("dataType");
			String dataStore = request.getParameter("dataStore");
			String dataShow = request.getParameter("dataShow");
			String dataLimit = request.getParameter("dataLimit");
			String dataStatus = request.getParameter("dataStatus");

			Map<Object, Object> map =new HashMap<>();
			map.put("datatabId", datatabId);
			map.put("datatabEname", datatabEname);
			map.put("datatabCname", datatabCname);
			map.put("dataEname", dataEname);
			map.put("dataCname", dataCname);
			map.put("dataType", dataType);
			map.put("dataStore", dataStore);
			map.put("dataShow", dataShow);
			map.put("dataLimit", dataLimit);
			map.put("dataStatus", dataStatus);

			
			//2、查询数据结构表列表
			List<DatagroupdInfo> datagroupdInfoList = datagroupdInfoService.findDatagroupdInfoByPage(page,rows,map);
			log.info("datagroupdInfoList={}",datagroupdInfoList);
			List<DatagroupdInfoVO> datagroupdInfoVOList = datagroupdInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<DatagroupdInfoVO> result = new PageResult<DatagroupdInfoVO>(datagroupdInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param datagroupdInfoVO
	 * @param datagroupdInfo
	 * @return
	 */
	public void changeVOToPO(DatagroupdInfoVO datagroupdInfoVO,DatagroupdInfo datagroupdInfo){
			BeanUtils.copyProperties(datagroupdInfoVO,datagroupdInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param datagroupdInfoVO
	 * @param datagroupdInfo
	 * @return
	 */
	public void changePoToVO(DatagroupdInfoVO datagroupdInfoVO,DatagroupdInfo datagroupdInfo){
			BeanUtils.copyProperties(datagroupdInfo,datagroupdInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param datagroupdInfo
	 * @return
	 */
	public DatagroupdInfoVO changePoToVO(DatagroupdInfo datagroupdInfo){
			DatagroupdInfoVO datagroupdInfoVO=new DatagroupdInfoVO();
			BeanUtils.copyProperties(datagroupdInfo,datagroupdInfoVO);
			

			return datagroupdInfoVO;
	}

	/**
	 * 3.查询数据结构表--详情页
	 * @param dataId
	 * @return
	 */
	@RequestMapping("/queryForDatagroupdInfoDetail")
	public ModelAndView queryForDatagroupdInfoDetail(@RequestParam(value="dataId",required=false) String dataId){
			//1.校验id
			CommonUtils.validateExistID(dataId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("dataId", dataId);
			log.info("查询数据结构表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			DatagroupdInfo datagroupdInfo = datagroupdInfoService.findByDatagroupdInfoId(map);
			
			DatagroupdInfoVO datagroupdInfoVO = changePoToVO(datagroupdInfo);
			//3、返回前台
			log.info("查询数据结构表查询成功：datagroupdInfo={}",datagroupdInfo);
			modelAndView.setViewName("system/datagroupd/queryForDatagroupdInfoDetail");
			modelAndView.addObject("datagroupdInfoVO",datagroupdInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}
 
	/**
	 * 6.删除数据结构表操作
	 * @param dataId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByDatagroupdInfoId")
	public JsonResultUtil deleteByDatagroupdInfoId(@RequestParam(value="dataId",required=false) String dataId){
			//1.校验id
			CommonUtils.validateExistID(dataId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("dataId", dataId);
			datagroupdInfoService.findByDatagroupdInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除数据结构表入参，map={}",map);
			
			try{
				datagroupdInfoService.upDelByDatagroupdInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除数据结构表操作---批量删除
	 * @param dataIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteByDatagroupdInfoIdBatch")
	public JsonResultUtil deleteByDatagroupdInfoIdBatch(@RequestParam(value="dataIdList",required=false) List dataIdList){
			//1.校验id
			CommonUtils.validateExistIDList(dataIdList);
			
			//2.校验数据库
			datagroupdInfoService.findByDatagroupdInfoIdList(dataIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("dataIdList", dataIdList);
			map.put("updateBy","000");
			log.info("批量删除数据结构表操作入参，map={}",map);
			
			try{
				datagroupdInfoService.upDelByDatagroupdInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}